//
//  AppSettings.m
//  BaseLib
//
//  Created by CPX on 2018/5/28.
//  Copyright © 2018年 CPX. All rights reserved.
//

#import "AppSettings.h"
#import <UIKit/UIKit.h>
#import "GoogleMaps/GoogleMaps.h"
#import "IQKeyboardManager.h"
#import "Firebase.h"
#ifdef NSFoundationVersionNumber_iOS_9_x_Max
#import <UserNotifications/UserNotifications.h>
#endif
#import "FBSDKApplicationDelegate.h"

NSString *const NotificationRegisterSuccess = @"notification_register_success";
NSString *const NotificationRegisterFailure = @"notification_register_failure";

/**
 应用级别设置，如通知
 应用权限申请，
 其他外部权限注册如google map
 @author yzl
 */
@implementation AppSettings

+ (void)registerApp:(UIApplication *)app delegate:(id)delegate options:(NSDictionary *)options{
//    [self registerLocalNotification];
    
    //google map service
    [GMSServices provideAPIKey:@"AIzaSyCG8_wLoPs35qlwxWpQLrHoPf5_4QVAq_E"];
    
    if ([[NSBundle mainBundle] pathForResource:@"GoogleService-Info" ofType:@"plist"]) {
        [FIRApp configure];
    }
    
    [IQKeyboardManager sharedManager].enable = NO;
//    [IQKeyboardManager sharedManager].shouldResignOnTouchOutside = YES;
    
//    [[FIRInstanceID instanceID] instanceIDWithHandler:^(FIRInstanceIDResult * _Nullable result, NSError * _Nullable error) {
//        if (error) {
//
//        }
//
//    }];
    [[FBSDKApplicationDelegate sharedInstance] application:app
                             didFinishLaunchingWithOptions:options];
    [FIRMessaging messaging].delegate = (id<FIRMessagingDelegate>)[UIApplication sharedApplication].delegate;
}

+ (void)registerLocalNotification{
    // 注册通知，如果已经获得发送通知的授权则创建本地通知，否则请求授权(注意：如果不请求授权在设置中是没有对应的通知设置项的，也就是说如果从来没有发送过请求，即使通过设置也打不开消息允许设置)
    if ([[UIApplication sharedApplication] currentUserNotificationSettings].types != UIUserNotificationTypeNone) {
    } else {
        [[UIApplication sharedApplication] registerUserNotificationSettings:[UIUserNotificationSettings settingsForTypes:UIUserNotificationTypeAlert|UIUserNotificationTypeBadge|UIUserNotificationTypeSound  categories:nil]];
    }
}

+ (void)registerFirebaseMessage{
    
    if (@available(iOS 10.0, *)){
        [UNUserNotificationCenter currentNotificationCenter];
        [UNUserNotificationCenter currentNotificationCenter].delegate = (id<UNUserNotificationCenterDelegate>)[UIApplication sharedApplication].delegate;
        UNAuthorizationOptions authOptions = UNAuthorizationOptionAlert |
        UNAuthorizationOptionSound | UNAuthorizationOptionBadge;
        [[UNUserNotificationCenter currentNotificationCenter]
         requestAuthorizationWithOptions:authOptions
         completionHandler:^(BOOL granted, NSError * _Nullable error) {
             // ...
         }];
    }
    else{
        UIUserNotificationType allNotificationTypes =
        (UIUserNotificationTypeSound | UIUserNotificationTypeAlert | UIUserNotificationTypeBadge);
        UIUserNotificationSettings *settings =
        [UIUserNotificationSettings settingsForTypes:allNotificationTypes categories:nil];
        [[UIApplication sharedApplication] registerUserNotificationSettings:settings];
    }
    
    [[UIApplication sharedApplication] registerForRemoteNotifications];
    
}

+ (void)onNotificationRegisterSuccess{
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationRegisterSuccess object:nil];
}

+ (void)onNotificationRegisterFailure{
    [[NSNotificationCenter defaultCenter] postNotificationName:NotificationRegisterSuccess object:nil];
}

@end
